home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * document.c
- *
- * Document handling routines.
- *
- *
- * Author: Rob Johnston
- * Date: Monday, February 10, 1992
- *
- * Copyright © 1992 Apple Computer, Inc.
- *
- */
-
-
- #include <Drag.h>
- #include "globals.h"
- #include "prototypes.h"
- #include "resources.h"
-
-
- extern pascal OSErr MyTrackingHandler(short message, WindowPtr theWindow,
- void *handlerRefCon, DragReference theDrag);
-
- extern pascal OSErr MyReceiveDropHandler(WindowPtr theWindow, void *handlerRefCon,
- DragReference theDrag);
-
-
- /*
- * AdjustDocumentView scrolls the document image by the appropriate amount
- * if the scroll bar value has changed.
- */
-
- void AdjustDocumentView(Document *theDocument)
-
- { short delta, docTop, docTopLimit;
-
- delta = (theDocument->vScrollPos - GetCtlValue(theDocument->vScroll)) *
- ScrollResolution;
-
- if (delta) {
- if (delta > 0) {
- docTop = (**(theDocument->theTE)).destRect.top;
- docTopLimit = (**(theDocument->theTE)).viewRect.top + TopMargin;
- if (docTop + delta > docTopLimit) {
- delta = docTopLimit - docTop;
- }
- }
- TEScroll(0, delta, theDocument->theTE);
- theDocument->vScrollPos = GetCtlValue(theDocument->vScroll);
- }
- }
-
-
- /*
- * AdjustScrollBar calculates the current position and maximum value of the
- * document's scroll bar given the current viewing position and document
- * text. This routine is called whenever the text is changed or the view
- * is auto-scrolled.
- */
-
- void AdjustScrollBar(Document *theDocument)
-
- { short docTop, docBottom, viewTop, viewBottom;
- short offTop, offBottom;
- RgnHandle viewRgn;
-
- docTop = (**(theDocument->theTE)).destRect.top;
- docBottom = docTop + TEGetHeight(32767, 0, theDocument->theTE);
- viewTop = (**(theDocument->theTE)).viewRect.top;
- viewBottom = (**(theDocument->theTE)).viewRect.bottom;
-
- offTop = ((viewTop - (docTop - TopMargin)) + ScrollResolution - 1) / ScrollResolution;
- offBottom = (((docBottom + BottomMargin) - viewBottom) + ScrollResolution - 1) / ScrollResolution;
- if (offTop < 0)
- offTop = 0;
- if (offBottom < 0)
- offBottom = 0;
-
- theDocument->vScrollPos = offTop;
-
- SetCtlMax(theDocument->vScroll, offTop + offBottom);
- SetCtlValue(theDocument->vScroll, offTop);
-
- viewRgn = NewRgn();
- RectRgn(viewRgn, &(**(theDocument->theTE)).viewRect);
- SectRgn(viewRgn, theDocument->hiliteRgn, theDocument->hiliteRgn);
- DisposeRgn(viewRgn);
- }
-
-
- /*
- * This routine computes the size and location of the window's scroll bars
- * and the TextEdit field given the current size of the window. This routine
- * is called to initialize these components of the window and to update
- * the position of these components when the window is resized.
- */
-
- void PositionDocumentParts(Document *theDocument)
-
- { Rect theRect;
-
- theRect = theDocument->theWindow->portRect;
- theRect.left = theRect.right - 15;
- theRect.right += 1;
- theRect.bottom -= 14;
- theRect.top -= 1;
- (**(theDocument->vScroll)).contrlRect = theRect;
-
- theRect = theDocument->theWindow->portRect;
- theRect.top = theRect.bottom - 15;
- theRect.bottom += 1;
- theRect.right -= 14;
- theRect.left -= 1;
- (**(theDocument->hScroll)).contrlRect = theRect;
-
- theRect = theDocument->theWindow->portRect;
- theRect.right -= 15;
- theRect.bottom -= 15;
- (**(theDocument->theTE)).viewRect = theRect;
- (**(theDocument->theTE)).destRect.right = theRect.right - RightMargin;
- TECalText(theDocument->theTE);
- }
-
-
- /*
- * NewDocument is called when a new document window is needed. Creation of
- * document data structures is handled here.
- */
-
- Document *NewDocument()
-
- { Document *theDocument;
- WindowPtr theWindow;
- Rect theRect = { 0, 0, 16, 16 };
- TextStyle theStyle;
- TEStyleHandle theStyleHandle;
- Point thePoint;
- // DragHandler theHandler;
- OSErr result;
-
- if (gDocumentCount == MaxDocumentCount)
- return((Document *) 0L);
-
- theDocument = gDocumentList[gDocumentCount++] =
- (Document *) NewPtr(sizeof(Document));
-
- theDocument->theWindow = theWindow =
- GetNewWindow(WindowTemplateID, 0L, (WindowPtr) -1L);
- ((WindowPeek) theWindow)->refCon = (long) theDocument;
- SetPort(theWindow);
-
- thePoint = *((Point *) (&theWindow->portRect.top));
- LocalToGlobal(&thePoint);
- if (thePoint.h < 10) {
- MoveWindow(theWindow, InitialH, InitialV, false);
- }
-
- theDocument->vScroll = NewControl(theWindow, &theRect, "", true, 0, 0, 0,
- scrollBarProc, (long) theDocument);
- theDocument->hScroll = NewControl(theWindow, &theRect, "", true, 0, 0, 0,
- scrollBarProc, (long) theDocument);
-
- theDocument->theTE = TEStylNew(&theRect, &theRect);
- (**(theDocument->theTE)).destRect.top = TopMargin;
- (**(theDocument->theTE)).destRect.left = LeftMargin;
- (**(theDocument->theTE)).destRect.bottom = 32767;
-
- TEAutoView(true, theDocument->theTE);
-
- TEFeatureFlag(teFOutlineHilite, TEBitSet, theDocument->theTE);
-
- theDocument->hiliteRgn = NewRgn();
- theStyleHandle = GetStylHandle(theDocument->theTE);
- (**theStyleHandle).teRefCon = (long) theDocument;
-
- theStyle.tsFont = 21;
- theStyle.tsSize = 12;
- TESetStyle(doFont + doSize, &theStyle, false, theDocument->theTE);
-
- theDocument->vScrollPos = 0;
- theDocument->fRefNum = 0;
- theDocument->dirty = false;
- theDocument->undoDragText = 0L;
-
- PositionDocumentParts(theDocument);
-
- InstallTrackingHandler(MyTrackingHandler, theWindow, (void *) theDocument);
- InstallReceiveHandler(MyReceiveDropHandler, theWindow, (void *) theDocument);
-
- return(theDocument);
- }
-
-
- /*
- * CloseDocument is called when a document window is being closed. Storage
- * of the document file and disposal of document data structures is handled
- * here.
- */
-
- void CloseDocument(Document *theDocument)
-
- { short result, index, response;
- Str255 theName, theVerb;
-
- index = 0;
- while ((gDocumentList[index] != theDocument) && (index < MaxDocumentCount))
- index++;
-
- if (gDocumentList[index] == theDocument) {
-
- if (theDocument->dirty) {
- GetWTitle(theDocument->theWindow, &theName);
- GetIndString(&theVerb, FileStringsID, (gQuitting) ? slQuittingIndex : slClosingIndex);
- ParamText(&theName, &theVerb, "", "");
- SetCursor(&qd.arrow);
- response = Alert(idSaveChangesALRT, 0L);
-
- if (response == 1) { /* Save */
- if (! DoSaveDocument(theDocument)) {
- gQuitting = false;
- return;
- }
- } else if (response == 3) { /* Don't Save */
- ;
- } else { /* Cancel */
- gQuitting = false;
- return;
- }
-
- }
-
- if (theDocument->fRefNum) {
- FSClose(theDocument->fRefNum);
- }
-
- RemoveTrackingHandler(MyTrackingHandler, theDocument->theWindow);
- RemoveReceiveHandler(MyReceiveDropHandler, theDocument->theWindow);
-
- DisposeRgn(theDocument->hiliteRgn);
- TEDispose(theDocument->theTE);
- DisposeWindow(theDocument->theWindow);
-
- if (theDocument->undoDragText) {
- DisposHandle(theDocument->undoDragText);
- theDocument->undoDragText = 0L;
- }
-
- while (index < MaxDocumentCount) {
- gDocumentList[index] = gDocumentList[index + 1];
- index++;
- }
-
- DisposPtr((Ptr) theDocument);
- gDocumentCount--;
- }
- }
-
-
- /*
- * DoActivateDocument is called when an event is received that reports that
- * a document window is being either activated or deactivated.
- */
-
- short DoActivateDocument(Document *theDocument, short activate)
-
- { WindowPtr theWindow;
-
- if (theDocument) {
- if (activate) {
- TEActivate(theDocument->theTE);
- HiliteControl(theDocument->vScroll, 0);
- HiliteControl(theDocument->hScroll, 0);
-
- TEGetHiliteRgn(theDocument->hiliteRgn, theDocument->theTE);
-
- } else {
- TEDeactivate(theDocument->theTE);
- HiliteControl(theDocument->vScroll, 255);
- HiliteControl(theDocument->hScroll, 255);
- }
- }
- }
-
-
-
- /*
- * Closes all document windows.
- */
-
- void CloseAllDocuments()
-
- {
- while (gDocumentCount) {
- CloseDocument(gDocumentList[0]);
- }
- }
-
-
- /*
- * If the given WindowPtr is a pointer to a document window, this function
- * returns a pointer to a document data structure. If the window is not
- * a document window, the function returns NULL.
- */
-
- Document *IsDocumentWindow(WindowPtr theWindow)
-
- { short index = 0;
- Document *theDocument;
-
- theDocument = (Document *) (((WindowPeek) theWindow)->refCon);
-
- while ((gDocumentList[index] != theDocument) && (index < gDocumentCount))
- index++;
-
- if (gDocumentList[index] == theDocument)
- return(theDocument);
- else
- return((Document *) 0L);
- }
-
-
- /*
- * DoFontSelection is called when the user chooses a font from the font menu.
- */
-
- short DoFontSelection(short fontNumber)
-
- { TextStyle theStyle;
- WindowPtr theWindow;
- Document *theDocument;
-
- if (theWindow = FrontWindow()) {
- if (theDocument = IsDocumentWindow(theWindow)) {
- theStyle.tsFont = fontNumber;
- TESetStyle(doFont, &theStyle, true, theDocument->theTE);
- }
- }
- }
-
-
- /*
- * DoSizeSelection is called when the user selects a font size from the
- * Size menu.
- */
-
- short DoSizeSelection(short fontSize)
-
- { TextStyle theStyle;
- WindowPtr theWindow;
- Document *theDocument;
-
- if (theWindow = FrontWindow()) {
- if (theDocument = IsDocumentWindow(theWindow)) {
- theStyle.tsSize = fontSize;
- TESetStyle(doSize, &theStyle, true, theDocument->theTE);
- }
- }
- }
-
-
- /*
- * DoStyleSelection is called when the user chooses a style from the
- * Style menu.
- */
-
- short DoStyleSelection(short styleItem)
-
- { TextStyle theStyle;
- WindowPtr theWindow;
- Document *theDocument;
-
- if (theWindow = FrontWindow()) {
- if (theDocument = IsDocumentWindow(theWindow)) {
- theStyle.tsFace = 0;
- if (styleItem > 1) {
- BitSet(&theStyle.tsFace, 9 - styleItem);
- TESetStyle(doFace + doToggle, &theStyle, true, theDocument->theTE);
- } else {
- TESetStyle(doFace, &theStyle, true, theDocument->theTE);
- }
- }
- }
- }
-
-
- /*
- * DoSelectAllDocument is called when the Select All menu command is chosen.
- */
-
- short DoSelectAllDocument(Document *theDocument)
-
- {
- if (theDocument) {
- TESetSelect(0, 32767, theDocument->theTE);
- }
- }
-
-
- /*
- * DisableUndoDrag is called when the last drag can no longer be undone.
- * This routine disposes of undo text allocations.
- */
-
- short DisableUndoDrag()
-
- { short index;
- Document *theDoc;
-
- gCanUndoDrag = slCantUndo;
-
- index = gDocumentCount;
- while (index--) {
- theDoc = gDocumentList[index];
- if (theDoc->undoDragText) {
- DisposHandle(theDoc->undoDragText);
- theDoc->undoDragText = 0L;
- }
- }
- }
-
-
- /*
- * DoUndoDrag is called when the user chooses Undo Drag.
- */
-
- short DoUndoDrag()
-
- { short index, selStart, selEnd;
- Document *theDoc;
- Handle theText;
- long theSize;
- WindowPtr theWindow;
- Rect theRect;
-
- if (gCanUndoDrag != slCantUndo) {
-
- theWindow = 0L;
- index = gDocumentCount;
- while (index--) {
- theDoc = gDocumentList[index];
- SetPort(theDoc->theWindow);
-
- if (theText = theDoc->undoDragText) {
-
- theDoc->undoDragText = (**theDoc->theTE).hText;
- (**theDoc->theTE).hText = theText;
-
- TECalText(theDoc->theTE);
-
- selStart = theDoc->undoSelStart;
- selEnd = theDoc->undoSelEnd;
- TESetSelect(selStart, selEnd, theDoc->theTE);
- theDoc->undoSelStart = theDoc->lastSelStart;
- theDoc->undoSelEnd = theDoc->lastSelEnd;
- theDoc->lastSelStart = selStart;
- theDoc->lastSelEnd = selEnd;
-
- theRect = theDoc->theWindow->portRect;
- theRect.right -= 15;
- theRect.bottom -= 15;
- EraseRect(&theRect);
- TEUpdate(&theRect, theDoc->theTE);
- }
- }
-
- if (gCanUndoDrag == slUndoDrag)
- gCanUndoDrag = slRedoDrag;
- else
- gCanUndoDrag = slUndoDrag;
-
- theWindow = gUndoFrontmost;
- gUndoFrontmost = gLastFrontmost;
- gLastFrontmost = theWindow;
- }
- }
-
-
-
-
-
-
-
-